﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using BUS;
using DAO;
using DTO;
namespace baitaptuan5
{
    public partial class DanhSachHoaDon : Form
    {
        public DanhSachHoaDon()
        {
            InitializeComponent();

            hdbus = new HoaDonBUS();
            hddto = new HoaDonDTO();

            sachbus = new SachBUS();
            sachdto = new SachDTO();
            dtSach = sachbus.laybang();

            khbus = new KhachHangBUS();
            khdto = new KhachHangDTO();

            ctbus = new ChiTietHoaDonBUS();
            ctdto = new ChiTietHoaDonDTO();

            dtHK = new DataTable();
            dtHD = new DataTable();
            dtCT = new DataTable();
            loaddata();
            dem();
        }
        HoaDonBUS hdbus;
        HoaDonDTO hddto;
        KhachHangBUS khbus;
        KhachHangDTO khdto;
        ChiTietHoaDonBUS ctbus;
        ChiTietHoaDonDTO ctdto;
        SachBUS sachbus;
        SachDTO sachdto;
        DataTable dtHK, dtHD, dtCT,dtSach;

        int idem, tien;
        void dem()
        {
            idem = 0;
            dataGridView1.DataSource = dtHD;
            foreach (DataRow row in dtHD.Rows)
            {
                idem++;
            }
            toolStripStatusLabel2.Text = idem.ToString();
        }

        void TongTien()
        {

            dtCT = ctbus.LayTableTheoHoaDon(hddto.MaHoaDon);
            dataGridView2.DataSource = dtCT;
            tien = 0;
            idem = 0;
            foreach (DataRow row in dtCT.Rows)
            {
                string filterExpression = "";

                filterExpression = "MaSach =" + row["MaSach"].ToString();

                DataRow[] rows = dtSach.Select(filterExpression);
                tien = int.Parse(rows[0].ItemArray[5].ToString()) * int.Parse(row["slban"].ToString());
                idem = idem + tien;

            }
            tongtienhd.Text = "TỔNG TIỀN :"+ idem.ToString();

        }
        void loaddata()
        {
            // load  text box
            AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
            textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
            textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            dtHK = khbus.LayTable();
            foreach (DataRow row in dtHK.Rows)
            {
                auto.Add(row["TenKhachHang"].ToString());
            }
            textBox1.AutoCompleteCustomSource = auto;
            //load data hoa  don 
            dtHD = hdbus.LayTable();
            dataGridView1.DataSource = dtHD;
            //load data  chi tiet
            dtCT = ctbus.LayTable();
            dataGridView2.DataSource = dtCT;
            dem();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string filterExpression = "";

                filterExpression = "TenKhachHang='" + textBox1.Text+"'";

                DataRow[] rows = dtHK.Select(filterExpression);
                dtHD = hdbus.LayTableTHeoKhacHang(int.Parse(rows[0].ItemArray[0].ToString()));
                dataGridView1.DataSource = dtHD;
                dem();
            }
            catch(Exception)
            {
            }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                int r = this.dataGridView1.CurrentCell.RowIndex;
                hddto = new HoaDonDTO();
                hddto.MaHoaDon= int.Parse(dataGridView1.Rows[r].Cells[0].Value.ToString());
                hddto.MaKhachHang = int.Parse(dataGridView1.Rows[r].Cells[1].Value.ToString());
                hddto.NgayLap = DateTime.Parse(dataGridView1.Rows[r].Cells[3].Value.ToString());
               // MessageBox.Show(dataGridView1.Rows[r].Cells[0].Value.ToString() + dataGridView1.Rows[r].Cells[1].Value.ToString() + dataGridView1.Rows[r].Cells[2].Value.ToString() + dataGridView1.Rows[r].Cells[3].Value.ToString() + dataGridView1.Rows[r].Cells[4].Value.ToString() + dataGridView1.Rows[r].Cells[5].Value.ToString(), "");
                TongTien();
            }
            catch (Exception)
            {
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            dtHD = hdbus.LayTableTHeongay(dateTimePicker1.Value, dateTimePicker2.Value);
            dataGridView1.DataSource = dtHD;
            dem();
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            dtHD = hdbus.LayTableTHeongay(dateTimePicker1.Value, dateTimePicker2.Value);
            dataGridView1.DataSource = dtHD;
            dem();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            LapHoaDon lap = new LapHoaDon(hddto);
            lap.ShowDialog();
            loaddata();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            hddto = new HoaDonDTO();
            LapHoaDon lap = new LapHoaDon(hddto);
            lap.ShowDialog();
            loaddata();
        }
    }
}
